package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.board.BoardPo;
import com.csii.adhoc.board.service.BoardService;
import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.mapper.TMgnDirMapper;
import com.csii.adhoc.dataready.service.TMgnDirService;
import com.csii.adhoc.dataready.TMgnDirPo;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * @description: 数据预览接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tMgnDirService")
@Slf4j
public class TMgnDirServiceImpl implements TMgnDirService {

    @Resource
    private TMgnDirMapper tMgnDirMapper;
    @Resource
    private UidGenerator uidGenerator;
    @Resource
    private BoardService boardService;

    @Override
    @Transactional
    public void tMgnDirMaintain(TMgnDirPo tMgnDirPo, String optMode) {
        //如果是新增仪表盘，就赋值主键ID
        if (CommonConstant.OPT_MODE_ADD.equals(optMode)) {
            tMgnDirPo.setMenuId(uidGenerator.getUID() + "");
        }
        //检查上传的值
        if (tMgnDirPo.getMenuId().isEmpty() ||
                tMgnDirPo.getMenuId() == null) {
            log.error("id不能为空，menu_id:" + tMgnDirPo.getMenuId());
            throw new ParamValidityException("id不能为空，menu_id:" + tMgnDirPo.getMenuId());
        }
        if (optMode.equals(TDataConstant.OPT_MODE_ADD) ||
                optMode.equals(TDataConstant.OPT_MODE_UPDATE)) {
            if (tMgnDirPo.getParentId() == null || tMgnDirPo.getParentId().isEmpty()) {
                log.error("父Id不能为空，ParentId:" + tMgnDirPo.getParentId());
                throw new ParamValidityException("父Id不能为空，ParentId:" + tMgnDirPo.getParentId());
            }
            if (!"0".equals(tMgnDirPo.getParentId())) {
                TMgnDirPo tmd = selectTMgnDirByPrimaryKey(tMgnDirPo.getParentId());
                if (tmd == null) {
                    log.error("未找到上级id:" + tMgnDirPo.getParentId());
                    throw new ParamValidityException("未找到上级id:" + tMgnDirPo.getParentId());
                }
                if ("2".equals(tmd.getAttr())) {
                    log.error("上级id只能是分组或目录,parentId:" + tMgnDirPo.getParentId());
                    throw new ParamValidityException("上级id只能是分组或目录,parentId:" + tMgnDirPo.getParentId());
                }
            }
            //1-数据集，2-目录，3-菜单,4-数据连接，5-外部连接,6-仪表盘
            if (tMgnDirPo.getType() == null ||
                    !("1".equals(tMgnDirPo.getType()) ||
                            "11".equals(tMgnDirPo.getType()) ||
                            "12".equals(tMgnDirPo.getType()) ||
                            "13".equals(tMgnDirPo.getType()) ||
                            "14".equals(tMgnDirPo.getType()) ||
                            "2".equals(tMgnDirPo.getType()) ||
                            "3".equals(tMgnDirPo.getType()) ||
                            "4".equals(tMgnDirPo.getType()) ||
                            "5".equals(tMgnDirPo.getType()) ||
                            "6".equals(tMgnDirPo.getType()))) {
                log.error("类型值不匹配 type:" + tMgnDirPo.getType());
                throw new ParamValidityException("类型值不匹配 type:" + tMgnDirPo.getType());
            }
            //1-节点，2-叶子
            if (tMgnDirPo.getAttr() == null ||
                    !("1".equals(tMgnDirPo.getAttr()) ||
                            "2".equals(tMgnDirPo.getAttr()))) {
                log.error("属性值不匹配 attr:" + tMgnDirPo.getAttr());
                throw new ParamValidityException("属性值不匹配 Attr:" + tMgnDirPo.getAttr());
            }
            //不能为空的值
            if (tMgnDirPo.getName().isEmpty() ||
                    tMgnDirPo.getName() == null) {
                log.error("名称不能为空，Name:" + tMgnDirPo.getName());
                throw new ParamValidityException("名称不能为空，Name:" + tMgnDirPo.getName());
            }
           /* if(tMgnDirPo.getCreatedBy().isEmpty()||
                    tMgnDirPo.getCreatedBy()==null) {
                log.error("柜员号不能为空，CreatedBy:" + tMgnDirPo.getCreatedBy());
                throw new ParamValidityException("柜员号不能为空，CreatedBy:" + tMgnDirPo.getCreatedBy());
            }*/
        }

        switch (optMode) {
            case TDataConstant.OPT_MODE_ADD:
                tMgnDirPo.setCreatedBy(tMgnDirPo.getUpdatedBy()).setCreatedDt(tMgnDirPo.getUpdatedDt());
                insertOneTMgnDir(tMgnDirPo);
                break;
            case TDataConstant.OPT_MODE_UPDATE:
                updateTMgnDirByPrimaryKey(tMgnDirPo);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteTMgnDirByPrimaryKey(tMgnDirPo.getMenuId());
                deleteBoardByTMgnDirPo(tMgnDirPo);
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
        }
    }

    private void deleteBoardByTMgnDirPo(TMgnDirPo tMgnDirPo) {
        //type等于6为仪表盘、只有叶子才是仪表盘attr=2
        if ("6".equals(tMgnDirPo.getType()) && "2".equals(tMgnDirPo.getAttr())) {
            BoardPo boardPo = new BoardPo();
            boardPo.setBoardId(tMgnDirPo.getMenuId());
            boardService.boardMaintain(boardPo, CommonConstant.OPT_MODE_DELETE);
        }
    }

    @Transactional
    @Override
    public void insertOneTMgnDir(TMgnDirPo tMgnDirPo) {
        if (StringUtils.isEmpty(tMgnDirPo.getMenuId())) {
            tMgnDirPo.setMenuId(UUID.randomUUID().toString().replace("-", ""));
        }
        if (StringUtils.isEmpty(tMgnDirPo.getAttr())) {
            tMgnDirPo.setAttr(tMgnDirPo.getName());
        }
        tMgnDirPo.setCreatedDt(tMgnDirPo.getUpdatedDt());
        if (tMgnDirPo.getType().length() > 1) {
            tMgnDirPo.setType(tMgnDirPo.getType().substring(0, 1));
        }
        tMgnDirMapper.insertOneTMgnDir(tMgnDirPo);
        //如果是新增仪表盘,需要往仪表盘表插入一条记录
        if ("6".equals(tMgnDirPo.getType()) && "2".equals(tMgnDirPo.getAttr())) {
            boardService.addBoardFromDir(tMgnDirPo);
        }
    }

    @Transactional
    @Override
    public int updateTMgnDirByPrimaryKey(TMgnDirPo tMgnDirPo) {
        if ("6".equals(tMgnDirPo.getType()) && "2".equals(tMgnDirPo.getAttr())
                && !StringUtils.isEmpty(tMgnDirPo.getName())) {
            BoardPo boardPo = new BoardPo();
            boardPo.setBoardId(tMgnDirPo.getMenuId()).setName(tMgnDirPo.getName());
            boardService.updateBoardPoByPrimaryKey(boardPo);
        }
        return tMgnDirMapper.updateByPrimaryKeySelective(tMgnDirPo);
    }

    @Transactional
    @Override
    public int deleteTMgnDirByPrimaryKey(String menuId) {
        return tMgnDirMapper.deleteByPrimaryKey(menuId);
    }

    @Override
    public TMgnDirPo selectTMgnDirByPrimaryKey(String menuId) {
        return tMgnDirMapper.selectByPrimaryKey(menuId);
    }

    @Override
    public List<TMgnDirPo> selectListTMgnDir(TMgnDirPo tMgnDirPo) {
        return tMgnDirMapper.selectListTMgnDir(tMgnDirPo);
    }
}
